package com.sxj.ums.controller;

import com.sxj.ums.constant.ResponseConstant;
import com.sxj.ums.entity.User;
import com.sxj.ums.exception.PasswordErrorException;
import com.sxj.ums.exception.UserNotExistException;
import com.sxj.ums.service.UserService;
import com.sxj.ums.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 石小俊
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ResponseResult login(String username,String password){
        ResponseResult result = new ResponseResult();

        try {
            User user = userService.login(username,password);
            result.setResponseCode(ResponseConstant.RESPONSE_CODE_SUCCESS);
            result.setMessage(ResponseConstant.RESPONSE_MESSAGE_SUCCESS);
            result.setReturnObject(user);
        } catch (UserNotExistException e) {
            result.setResponseCode(ResponseConstant.RESPONSE_CODE_USERNAME_ERROR);
            result.setMessage(e.getMessage());
        } catch (PasswordErrorException e) {
            result.setResponseCode(ResponseConstant.RESPONSE_CODE_PASSWORD_ERROR);
            result.setMessage(e.getMessage());
        }

        return result;
    }
}
